﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Controls_VIPSEARCH : System.Web.UI.UserControl
{
    public event RepeaterCommandEventHandler ItemCommand;
    //public List<string> Selected
    //{
    //    get
    //    {
    //        List<string> l = new List<string>();
    //        foreach (RepeaterItem item in rptVIP.Items)
    //        {
    //            CheckBox cb = item.FindControl("chkChoose") as CheckBox;
    //            if (cb != null && cb.Checked)
    //            {
    //                HiddenField fld = item.FindControl("hfvip") as HiddenField;
    //                if (fld != null)
    //                {
    //                    string id = fld.Value;
    //                    l.Add(id);
    //                }
    //            }
    //        }
    //        return l;
    //    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            V_CardSet();
            TextBox5.Focus();
        }
        if (rptVIP.Items.Count> 0)
        {
            Button2.Focus();
        }
    }
    private void V_CardSet()
    {
        List<Model.V_CARSET> vl = new BLL.V_CARSET().GetAllList();
        DropDownList1.DataSource = vl;
        DropDownList1.DataTextField = "MC";
        DropDownList1.DataValueField = "DM";
        DropDownList1.DataBind();
        ListItem li = new ListItem("", "-1");
        DropDownList1.Items.Insert(0, li);
    }
    private void Bind()
    {
        string StrSql = "select V_CUSTOMER.BYZD5,V_CUSTOMER.dm,cast(V_vipset.zk as varchar(10)) as zk ,V_vipset.MC ,V_vipset.dm as vipdm,isnull(V_CUSTOMER.gkmc,'')as gkmc,isnull(V_CUSTOMER.sj,'') as sj ,isnull(V_CUSTOMER.dh,'')as dh from V_CUSTOMER inner join V_vipset on V_vipset.gkdm=V_CUSTOMER.dm where 1=1 ";

        //string StrSql = "select V_CUSTOMER.dm+'|'+cast(V_vipset.zk as varchar(10))+'|'+V_vipset.MC as dm, V_CUSTOMER.dm+' '+V_vipset.dm+' '+isnull(V_CUSTOMER.gkmc,'')+' '+isnull(V_CUSTOMER.sj,'')+'  '+isnull(V_CUSTOMER.dh,'') as mc from V_CUSTOMER inner join V_vipset on V_vipset.gkdm=V_CUSTOMER.dm where 1=1 ";

        bool hasP = true ;   //默认只能精确查找


        string FCode = "";
        try
        {
               FCode = Session["FCode"].ToString();
            hasP = (!Check.CheckPermission(FCode, "00033"));
        }
        catch
        {
            hasP = true;
        }
        finally
        {
           // Response.Write("<script>alert(\'"+FCode+"\')</script>");
        }


        if (hasP)       //是否精确查找
        {
            if (DropDownList1.SelectedValue != "-1")
            {
                StrSql += " and V_vipset.kldm LIKE '%" + DropDownList1.SelectedValue + "%' ";
            }
            if (TextBox1.Text.Trim() == "" && TextBox2.Text.Trim() == "" && TextBox3.Text.Trim() == "" && TextBox4.Text.Trim() == "" &&
                TextBox5.Text.Trim() == "" && nickname.Text.Trim() == "")
            {
                StrSql += " and V_vipset.dm='' ";
                StrSql += " and V_CUSTOMER.gkmc='' ";
                StrSql += " and V_CUSTOMER.sj='' ";
                StrSql += " and V_CUSTOMER.dh='' ";
                StrSql += " and V_CUSTOMER.dm='' ";
                StrSql += " and V_CUSTOMER.BYZD5='' ";
            }
            else
            {
                if (TextBox1.Text.Trim() != "")
                {
                    StrSql += " and V_vipset.dm='" + TextBox1.Text.Trim() + "' ";
                }
                if (TextBox2.Text.Trim() != "")
                {
                    StrSql += " and V_CUSTOMER.gkmc='" + TextBox2.Text.Trim() + "' ";
                }
                if (TextBox3.Text.Trim() != "")
                {
                    StrSql += " and V_CUSTOMER.sj='" + TextBox3.Text.Trim() + "' ";
                }
                if (TextBox4.Text.Trim() != "")
                {
                    StrSql += " and V_CUSTOMER.dh='" + TextBox4.Text.Trim() + "' ";
                }
                if (TextBox5.Text.Trim() != "")
                {
                    StrSql += " and V_CUSTOMER.dm='" + TextBox5.Text.Trim() + "' ";
                }
                if (nickname.Text.Trim() != "")
                {
                    StrSql += " and V_CUSTOMER.BYZD5='" + nickname.Text.Trim() + "' ";
                }
            }


        }
        else
        {
            if (DropDownList1.SelectedValue != "-1")
            {
                StrSql += " and V_vipset.kldm LIKE '%" + DropDownList1.SelectedValue + "%' ";
            }
            if (TextBox1.Text.Trim() != "")
            {
                StrSql += " and V_vipset.dm LIKE '%" + TextBox1.Text.Trim() + "%' ";
            }
            if (TextBox2.Text.Trim() != "")
            {
                StrSql += " and V_CUSTOMER.gkmc like '%" + TextBox2.Text.Trim() + "%' ";
            }
            if (TextBox3.Text.Trim() != "")
            {
                StrSql += " and V_CUSTOMER.sj like '%" + TextBox3.Text.Trim() + "%' ";
            }
            if (TextBox4.Text.Trim() != "")
            {
                StrSql += " and V_CUSTOMER.dh like '%" + TextBox4.Text.Trim() + "%' ";
            }
            if (TextBox5.Text.Trim() != "")
            {
                StrSql += " and V_CUSTOMER.dm LIKE '%" + TextBox5.Text.Trim() + "%' ";
            }
            if (nickname.Text.Trim() != "")
            {
                StrSql += " and V_CUSTOMER.BYZD5 LIKE '%" + nickname.Text.Trim() + "%' ";
            }
        }
        DataTable vc = new BLL.V_CUSTOMER().GetListByPopWinSearch(StrSql);
        rptVIP.DataSource = vc.DefaultView;
        rptVIP.DataBind();

        //ListBox1.DataSource = vc.DefaultView;
        //ListBox1.DataTextField = "mc";
        //ListBox1.DataValueField = "dm";
        //ListBox1.DataBind();
        Button2.Focus();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind();
    }
    protected void rptVIP_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (ItemCommand != null)
            ItemCommand(source, e);
    }
}
